/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controlador;

import Beans.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author USUARIO
 */
public class ServletProgramacion extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String accion = request.getParameter("showIti");
        try {
            int idAge = Integer.parseInt(request.getParameter("idAge"));
            int idRG = Integer.parseInt(request.getParameter("idRG"));
            /* _________________________________________________________________ */
            String h = null;
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            ModIti_Agencias ag = new ModIti_Agencias().ObtAgenciaById(idAge);
            /* ----------------------------------------------------------------- */
            ModIti_Ruta ruta = new ModIti_Ruta().obtenerIdRutaByOri(ag.getCiudad());
            ArrayList<ModIti_Servicios> lisServ = new ModIti_Servicios().listarServiciosByIdRuta(ruta.getId());
            /*Style DataTales */
            out.println("<link href=\"../../css/datatables/dataTables.bootstrap.css\" rel=\"stylesheet\" type=\"text/css\" />");
            /* Java scritp y plugins DataTables */
            out.println("<script src=\"../../js/datatables/dataTables.bootstrap.js\" type=\"text/javascript\"></script>");
            out.println("<script src=\"../../js/datatables/jquery.dataTables.js\" type=\"text/javascript\"></script>");
            out.println("<script type=\"text/javascript\">");
            out.println("$(function() {");
            out.println("$(\"#example1\").dataTable();");
            out.println("$('#example2').dataTable({");
            out.println("\"bPaginate\": true,");
            out.println("\"bLengthChange\": false,");
            out.println("\"bFilter\": false,");
            out.println("\"bSort\": true,");
            out.println("\"bInfo\": true,");
            out.println("\"bAutoWidth\": false");
            out.println("});");
            out.println("});");
            out.println("</script>");
            /* ****************************************************************************************************** */
            out.println("<table id=\"example1\" class=\"table table-bordered table-striped\">");
                out.println("<thead>");
                    out.println("<tr>");
                        out.println("<th>Hora</th>");
                        out.println("<th>Servicio y Rutas</th>");
                        out.println("<th>Acciones de Programación</th>");
                    out.println("</tr>");
                out.println("</thead>");
                out.println("<tbody>");
            for(int i=0; i<lisServ.size(); i++)
            {   ArrayList<ModIti_Itinerario> lisIti = new ModIti_Itinerario().listarItinerariosByIdServicios(lisServ.get(i).getId());
                for(int j=0; j<lisIti.size(); j++)
                {   h = sdf.format(lisIti.get(j).getHorasalida().getTime());
                    boolean ok = new ModIti_Programacion().estaProgramadoByIdItinerario(lisIti.get(j).getIditinerario());
                    if(ok)
                    {   ModIti_Programacion pr = new ModIti_Programacion().obtenerProgramacionByIdItinerario(lisIti.get(j).getIditinerario());
                        ArrayList<ModIti_Dias> dias = new ModIti_Dias().obtenerDiaByIdProgramacion(pr.getIdprogramacion());
                out.println("<tr style=\"color:#04B404;font-weight:bold;\">");
                    out.println("<td>"+h+"</td>");
                    out.println("<td>Servicio "+lisServ.get(i).getNombre()+" &#8212; "+lisServ.get(i).getTiposervicio()+"&ensp; | &ensp;"+ruta.getOrigen()+" &#8212; "+ruta.getDestino()+"&ensp; | &ensp;");
                            for(int y=0; y<dias.size(); y++)
                            {   
                            out.println(dias.get(y).getDia().substring(0, 3)+" &ensp; ");
                            }
                            ModEmp_Empleado em = new ModEmp_Empleado().obtenerEmpleadoByID(pr.getFk_idempleado()); 
                            ModEmp_Persona per = new ModEmp_Persona().obtenerPersona(em.getFk_idpersona());
                            out.println(" | &ensp;"+per.getApepaterno()+" "+per.getApematerno()+" "+per.getNombre()+" &ensp; ");
                            ModVeh_Vehiculo v = new ModVeh_Vehiculo().ObtenerVehiculoById(pr.getFk_idvehiculo());
                            out.println(" | &ensp; Placa: "+v.getPlaca());
                    out.println("</td>");
                    out.println("<td>");
                        out.println("<a href=\"#\">");
                            out.println("<button type=\"button\" style=\"background:none;border:none;\" title=\"Editar Programación\">");
                                out.println("<img src=\"../../img/edit.png\" width=\"24\" height=\"24\"/>");
                            out.println("</button>");
                        out.println("</a>");
                    out.println("</td>");
                out.println("</tr>");
                    } else
                    {
                out.println("<tr style=\"color:red;font-weight:bold;\">");
                    out.println("<td>"+h+"</td>");
                    out.println("<td>Servicio "+lisServ.get(i).getNombre()+" &#8212; "+lisServ.get(i).getTiposervicio()+"&ensp; | &ensp;"+ruta.getOrigen()+" &#8212; "+ruta.getDestino()+"&ensp; | &ensp;");
                    out.println("</td>");
                    out.println("<td>");
                        out.println("<a href=\"programacionItinerario2.jsp?idI="+lisIti.get(i).getIditinerario()+"&idS="+lisServ.get(i).getId()+"&idR="+ruta.getId()+"&idRG="+idRG+"&idA="+idAge+"\">");
                            out.println("<button type=\"button\" style=\"background:none;border:none;\" title=\"Programar servicio\">");
                                out.println("<img src=\"../../img/schedule.png\" width=\"24\" height=\"24\"/>");
                            out.println("</button>");
                        out.println("</a>");
                    out.println("</td>");
                out.println("</tr>");
                    }
                }  
            }
                out.println("</tbody>");
                out.println("<tfoot>");
                    out.println("<tr>");
                        out.println("<th>Hora</th>");
                        out.println("<th>Servicio y Rutas</th>");
                        out.println("<th>Acciones de Programación</th>");
                    out.println("</tr>");
                out.println("</tfoot>");
            out.println("</table>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
